package com.tzxs.pay.main.base;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;

import com.tzxs.pay.R;
import com.tzxs.pay.main.print.utils.print.AidlUtil;
import com.tzxs.pay.util.SystemUtil;

import java.util.List;

public abstract class BaseActivity<VB extends ViewDataBinding> extends AppCompatActivity {
    protected VB dataBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(getChildActivity(), getLayoutId());
    }

    protected abstract Activity getChildActivity();

    protected abstract int getLayoutId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //   AidlUtil.getInstance().disconnectPrinterService(TzApplication.getInstance());
    }

    //获取打印图片方法
    protected int getResId(String bitmapType) {
        int resId = 0;

        switch (bitmapType) {
            //支付宝支付
            case "0":
                resId = R.drawable.print_alipay;
                break;
            //微信支付
            case "1":
                resId = R.drawable.print_wx;
                break;
            //银行卡支付
            case "2":
                resId = R.drawable.print_unicon;

                break;
        }
        return resId;
    }

    /**
     * 打印体通用方法
     * @param printContent 打印内容
     * @param aidlUtil 打印工具类
     * @param bitmapType 打印图片地址
     */
    protected void commenPrint(List<String> printContent, AidlUtil aidlUtil, String bitmapType) {
        if (SystemUtil.getSystemModel().contains("A8")) {
            aidlUtil.printBitmapForA8(bitmapType, 2);
        } else {
            int resId;
            resId = getResId(bitmapType);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
            aidlUtil.printBitmap(bitmap, "2", "2", resId);
        }
        //抬头

            aidlUtil.printText(printContent.get(1), "32", "32", "1",
                    true, false);
        //门店
        if (SystemUtil.getSystemModel().contains("P1")||SystemUtil.getSystemModel().contains("V1")){
            aidlUtil.printText("\n"+printContent.get(2)+"\n", "24", "24", "1",
                    false, false);
        }
        else {
            aidlUtil.printText(printContent.get(2), "24", "24", "1",
                    false, false);
        }


        //订单支付方式text开始
        if (SystemUtil.getSystemModel().contains("P1")||SystemUtil.getSystemModel().contains("V1")){
            if (printContent.get(3).substring(0,2).equals("\\n")){
                aidlUtil.printText(printContent.get(3).substring(2).replace("\\n", "\n"),
                        "24", "24", "0",
                        false, false);
            }
            else {
                aidlUtil.printText(printContent.get(3).replace("\\n", "\n"),
                        "24", "24", "0",
                        false, false);
            }

        }
        else {
            aidlUtil.printText(printContent.get(3).replace("\\n", "\n"),
                    "24", "24", "0",
                    false, false);
        }

        //业务记录

        aidlUtil.printText(printContent.get(4), "32", "32", "0",
                true, false);

         Log.i("2333",printContent.get(5));
        //业务记录内容
        if (!TextUtils.isEmpty(printContent.get(5))) {
            if (SystemUtil.getSystemModel().contains("A8")){
                if (printContent.get(5).substring(0,2).equals("\\n")){

                    aidlUtil.printText( printContent.get(5).substring(2)
                                    .replace("\\n", "\n"), "24", "24", "0",
                            false, false);
                }
                else {
                    aidlUtil.printText( printContent.get(5)
                                    .replace("\\n", "\n"), "24", "24", "0",
                            false, false);
                }

            }
            else {
                aidlUtil.printText( printContent.get(5)
                                .replace("\\n", "\n"), "24", "24", "0",
                        false, false);
            }

        }

//            if (SystemUtil.getSystemModel().contains("A8")){
//                aidlUtil.printText("",  "24", "24", "0",
//                        false, false);
//            }

        //退款 ||收款
        if (SystemUtil.getSystemModel().contains("P1")||SystemUtil.getSystemModel().contains("V1")){
            String content=printContent.get(6);

            aidlUtil.printText(  content, "32", "32", "0",
                    true, false);
        }
        else {
            aidlUtil.printText(  printContent.get(6), "32", "32", "0",
                    true, false);
        }

        //退款获取收款内容
        if (SystemUtil.getSystemModel().contains("P1")||SystemUtil.getSystemModel().contains("V1")){
            aidlUtil.printText("\n"+printContent.get(7)
                            .replace("\\n", "")+"\n", "24", "24", "0",
                    false, false);
        }
        else{
            aidlUtil.printText(printContent.get(7)
                            .replace("\\n", ""), "24", "24", "0",
                    false, false);
        }


        //打印二维码
        aidlUtil.printQr(printContent.get(8), "6", "3");
        //收银员  //打印时间
        if (SystemUtil.getSystemModel().contains("P1")||SystemUtil.getSystemModel().contains("V1")){
            aidlUtil.printText(printContent.get(9).replace("\\n", "\n")+"\n",
                    "24", "24", "1", false, false);
        }
        else {
            aidlUtil.printText(printContent.get(9).replace("\\n", "\n"),
                    "24", "24", "1", false, false);
        }

        if (SystemUtil.getSystemModel().contains("P1")||SystemUtil.getSystemModel().contains("V1")){
            aidlUtil.printText( printContent.get(10).replace("\\n", "")+"\n",
                    "24", "24", "1", false, false);
        }
        else {
            aidlUtil.printText( printContent.get(10).replace("\\n", ""),
                    "24", "24", "1", false, false);
        }

//            aidlUtil.printText(printContent.get(11).replace("\\n", "\n"),
//                    "24", "24", "1", false, false);
        aidlUtil.printText( printContent.get(12),
                "24", "24", "1", false, false);
        aidlUtil.printBlankLine("5", "30");
        aidlUtil.printCommit();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            if (mInputMethodManager == null) {
                return false;
            }
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }
}
